package com.example.smartcitydemo.repo.work


import com.example.smartcity_jscs.network.ResultStates.Resource
import com.example.smartcitydemo.base.BaseRepo
import com.example.smartcitydemo.bean.*
import com.example.smartcitydemo.network.client.authApiService
import com.example.smartcitydemo.network.client.noAuthApiService
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.*

/**
 * @PACKAGE_NAME: com.example.smartcity_jscs.repository.work
 * @NAME: Caleb.F
 * @DATE: 2021/8/31 9:33
 * @Email: 1919796283@qq.com
 */
object WorkRepo : BaseRepo() {

    suspend fun getJobType(): Flow<Resource<List<JobTypeBean>>> {
        return requestListData({ authApiService.getJobType() }, { it })
    }


    suspend fun getJobTypeByProfessionName(name: String): Flow<Resource<List<JobListBean>>> {
        val jobTypeByName = requestListData({ authApiService.getJobTypeByName(name) }, { it })
        return jobTypeByName.transform { rawData ->
            rawData.data?.run {
                map {
                    requestListData({ noAuthApiService.getJobListById(it.id) }, { it })
                }.merge().collect {
                    emit(it)
                }
            }
        }.flowOn(Dispatchers.IO)
    }

    suspend fun getJobListAll(): Flow<Resource<List<JobListBean>>> {
        return requestListData({ noAuthApiService.getJobListAll() }, { it })
    }

    suspend fun getJobListById(professionId: Int): Flow<Resource<List<JobListBean>>> {
        return requestListData({ noAuthApiService.getJobListById(professionId) }, { it })
    }

    suspend fun getCompanyDetail(id: Int): Flow<Resource<ComPanyDetail>> {
        return request({ noAuthApiService.getCompanyDetail(id) }, { it })
    }

    suspend fun getJobDetail(id: Int): Flow<Resource<JobListBean>> {
        return request({ noAuthApiService.getPostDetail(id) }, { it })
    }


    suspend fun postDeliver(deliverBean: DeliverPostBean): Flow<Resource<String>> {
        return flow {
            val postDeliver = authApiService.postDeliver(deliverBean)
            if (postDeliver.code == 200) {
                emit(Resource.Success("提交成功"))
            } else {
                handleResponseErrorCode(postDeliver.code, postDeliver.msg)
            }
        }.flowOn(Dispatchers.IO)
    }

    suspend fun getDeliverList(userId: Int): Flow<Resource<List<DeliverHistoryBean>>> {
        return requestListData({
            authApiService.getDeliverList()
        }, {
            it.filter { bean ->
                userId == bean.userId
            }.toList()
        })
    }


    suspend fun modifyPersonResume(bean: ResumeModifyBean): Flow<Resource<String>> {
        return flow {
            val postPersonResume = authApiService.postModifyResume(bean)
            if (postPersonResume.code == 200) {
                emit(Resource.Success("success"))
            } else {
                handleResponseErrorCode(postPersonResume.code, postPersonResume.msg)
            }
        }

    }

    suspend fun addPersonResume(bean: ResumePostBean): Flow<Resource<String>> {
        return requestCommonData { authApiService.postPersonResume(bean) }
    }


    suspend fun getPersonResumeInfo(): Flow<Resource<List<ResumeDetailBean>>> {
        return requestListData({ authApiService.getResumeList() }, { it })
    }

}